// pages/Index/willComment.js
const controller = require("../controllers/Controller").controller

Page({

  /**
   * 页面的初始数据
   */
  data: {
    icon1: "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210909/885563688243691520.png",
    icon2: "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210914/887356889128501248.png",

    imgList: [],
    comment: "",
    finalScore: 5,
    shopScore: 5,
    anonymous: 0,
    chooseProduct: [],
    goodsType: 1
  },

  //恢复默认
  setDefault() {
    this.setData({
      imgList: [],
      comment: '',
      finalScore: 5,
      shopScore: 5,
      anonymous: 0,
      chooseProduct: []
    })
  },

  //发布评价
  commentShop() {
    let value = {}
    if (this.data.chooseProduct.length > 0) {
      value.goodsName = this.data.chooseProduct[0].name
      value.goodsImage = this.data.chooseProduct[0].image
      value.goodsPrice = this.data.chooseProduct[0].price
      value.goodsVipPrice = this.data.chooseProduct[0].vipPrice
      value.goodsId = this.data.chooseProduct[0].id
    }
    value.shopId = wx.getStorageSync('shopId')
    value.comment = this.data.comment
    value.finalScore = this.data.finalScore
    value.shopScore = this.data.shopScore
    value.goodsType = this.data.goodsType
    value.anonymous = this.data.anonymous
    value.commentImages = this.data.imgList.join(",")
    controller.commentShop(value).then(res => {
      wx.showToast({
        title: '操作成功',
      })
      setTimeout(function () {
        wx.navigateBack()
      }, 1500)
    })
  },


  //删除选中商品
  delGoods(e) {
    let index = e.currentTarget.dataset.index
    let list = this.data.chooseProduct
    wx.showModal({
      title: "是否删除该商品",
      success: (res) => {
        if (res.confirm) {
          list.splice(index, 1)
          this.setData({
            chooseProduct: list
          })
        }
      }
    })

  },


  //跳转添加商品页面
  toSelectGoods() {
    let that = this
    wx.navigateTo({
      url: '/pages/Index/selectGoods?nowList=' + JSON.stringify(that.data.chooseProduct),
    })
  },

  //设置是否匿名评价
  setAnonymous() {
    if (this.data.anonymous == 0) {
      this.setData({
        anonymous: 1
      })
    } else {
      this.setData({
        anonymous: 0
      })
    }
  },

  //设置评分
  setScore(e) {
    let index = e.currentTarget.dataset.index
    if (index < this.data.finalScore) {
      this.setData({
        finalScore: index,
        shopScore: index
      })
    } else {
      this.setData({
        finalScore: index + 1,
        shopScore: index + 1
      })
    }

  },

  //删除将要上传的图片
  deleteImg(e) {
    let index = e.currentTarget.dataset.index;
    let imgList = this.data.imgList;
    imgList.splice(index, 1);
    this.setData({
      imgList: imgList
    })
  },

  // 图片上传
  uploadImg() {
    if (this.data.imgList.length < 9) {
      let that = this;
      wx.chooseImage({
        success(res) {
          const tempFilePaths = res.tempFilePaths;
          const tempFiles = res.tempFiles;
          if (tempFiles[0].size > 1024 * 1024) {
            wx.showToast({
              title: '单张图片大不能超过1MB',
              icon: 'none',
              duration: 2000
            })
            return
          }
          for (let i = 0; i < 9 - that.data.imgList.length; i++) {
            wx.uploadFile({
              url: 'https://www.xlshhr.com/beauty-industry/api/img/uploadFile',
              filePath: tempFilePaths[i],
              name: 'image',
              formData: {
                'sessionId': that.data.token
              },
              success(res) {
                console.log(res);
                // const data = res.date
                let src = JSON.parse(res.data)
                let srcList = that.data.imgList;
                srcList.push(src.data);
                that.setData({
                  imgList: srcList
                })
              }
            })
          }
        }
      })
    }
  },

  //获取评论内容
  getCommentText(e) {
    this.setData({
      comment: e.detail.value
    })
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})